﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProdCalcTool.PdcSvc;
using ProdCalcTool.PrcSvc;

namespace ProdCalcTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        CalcItem GetPriceInfo(int sku)
        {
            using (ProductCatalogServiceClient client = new ProductCatalogServiceClient())
            {
                client.ClientCredentials.UserName.UserName = "pum.go";
                client.ClientCredentials.UserName.Password = "Dx+12345";

                GetProductRequest req = new GetProductRequest()
                {
                    SKU = sku,
                    Lang = "EN",
                };

                GetProductResponse resp = client.GetProduct(req);
                return new CalcItem()
                {
                    PackagingCost = resp.Product.PackagingCost.Value,
                    GrossWeight = resp.Product.GrossWeight,
                    UnitCost = resp.Product.UnitCost,
                    Price = resp.Product.Price,
                    ListPrice = resp.Product.ListPrice,
                    PackageType = resp.Product.PackageType,
                    PackageBag=resp.Product.PackageBag,
                    PackagingHeight=resp.Product.PackagingHeight,
                    PackagingLength=resp.Product.PackagingLength,
                    PackagingWidth=resp.Product.PackagingWidth,
                    PrimaryCategoryID=resp.Product.PrimaryCategoryId,
                    ProductProfitMargin=resp.Product.ProductProfitMargin,
                };


            }
        }

        CalcItem calcItem;

        private void btnGetProdInfo_Click(object sender, EventArgs e)
        {
            calcItem = GetPriceInfo(Convert.ToInt32(txtSKU.Text));

            txtGrossWeight.Text = txtCalculatedLocalPrice.Text = txtPkgCost.Text =
            txtUnitCost.Text = txtCalculatedAUPrice.Text = string.Empty;

            txtUnitCost.Text = calcItem.UnitCost.ToString("f2");
            txtPkgCost.Text = calcItem.PackagingCost.ToString("f2");
            txtGrossWeight.Text = calcItem.GrossWeight.ToString("f2");
            txtPrice.Text = calcItem.Price.value.ToString("f2");
            txtListPrice.Text = calcItem.ListPrice.value.ToString("f2");

        }

        private void btnCalcLocalPrice_Click(object sender, EventArgs e)
        {
            txtCalculatedAUPrice.Text = string.Empty;

            //CalcItem calcItem = new CalcItem()
            //{
            //    UnitCost = Convert.ToDouble(txtUnitCost.Text),
            //    GrossWeight = Convert.ToDouble(txtGrossWeight.Text),
            //    PackagingCost = Convert.ToDouble(txtPkgCost.Text),
            //};

            //txtCalculatedLocalPrice.Text = Math.Round(calcItem.CalculatedLocalPrice,1,MidpointRounding.AwayFromZero).ToString("f2");

            using (PriceAdminServiceClient client = new PriceAdminServiceClient())
            {
                client.ClientCredentials.UserName.UserName = "pum.go";
                client.ClientCredentials.UserName.Password = "Dx+12345";

                CalculateSalesPriceRequest req = new CalculateSalesPriceRequest()
                {
                    CalculateProductSalesPrice = new CalculateProductSalesPrice()
                    {
                        GrossWeight = calcItem.GrossWeight,
                        HandlingCost=calcItem.HandlingCost,
                        MSRPPrice=calcItem.MSRPPrice,
                        PackageBag=calcItem.PackageBag,
                        PackageType = Convert2Enum(calcItem.PackageType, PackageType.Unknown),
                        PackagingCost=Convert.ToDecimal(calcItem.PackagingCost),
                        PackagingHeight=calcItem.PackagingHeight,
                        PackagingWidth=calcItem.PackagingWidth,
                        PackagingLength=calcItem.PackagingLength,
                        UnitCost=Convert.ToDecimal(calcItem.UnitCost),
                        PrimaryCategoryID=calcItem.PrimaryCategoryID,
                        ProductProfitMargin=calcItem.ProductProfitMargin,
                    },
                };

                CalculateSalesPriceResponse resp = client.CalculateSalesPrice(req);
                txtCalculatedLocalPrice.Text = resp.Price.ToString();

                //calcItem.CalculatedAUPrice = resp.SkuPriceDatas[0].DirectPrice;
                //txtCalculatedAUPrice.Text = resp.SkuPriceDatas[0].DirectPrice.ToString("f2");
            }
        }

        private void btnCalcAUPrice_Click(object sender, EventArgs e)
        {
            //CalcItem calcItem = new CalcItem();
            using (PriceAdminServiceClient client = new PriceAdminServiceClient())
            {
                client.ClientCredentials.UserName.UserName = "pum.go";
                client.ClientCredentials.UserName.Password = "Dx+12345";

                CalculateDirectPriceRequest req = new CalculateDirectPriceRequest()
                {
                    SiteID = 1,
                    SkuInfos = new ProdCalcTool.PrcSvc.SkuInfo[1]
                    {
                        new ProdCalcTool.PrcSvc.SkuInfo()
                    {
                        Sku = Convert.ToInt32(txtSKU.Text),
                        UnitCost = Convert.ToDecimal(txtUnitCost.Text),
                        GrossWeight = Convert.ToDouble(txtGrossWeight.Text),
                        PrimaryCategoryId= 400,
                        UnitPrice = Convert.ToDecimal(txtCalculatedLocalPrice.Text),
                    }
                    },
                    WHID = 103,

                };

                CalculateDirectPriceResponse resp = client.CalculateDirectPrice(req);

                //calcItem.CalculatedAUPrice = resp.SkuPriceDatas[0].DirectPrice;
                txtCalculatedAUPrice.Text = resp.SkuPriceDatas[0].DirectPrice.ToString("f2");
            }

            //txtCalculatedAUPrice.Text = calcItem.CalculatedAUPrice.ToString("f2");

        }

        private void btnCalcUSPrice_Click(object sender, EventArgs e)
        {
            using (PriceAdminServiceClient client = new PriceAdminServiceClient())
            {
                client.ClientCredentials.UserName.UserName = "pum.go";
                client.ClientCredentials.UserName.Password = "Dx+12345";

                CalculateDirectPriceRequest req = new CalculateDirectPriceRequest()
                {
                    SiteID = 1,
                    SkuInfos = new ProdCalcTool.PrcSvc.SkuInfo[1]
                    {
                        new ProdCalcTool.PrcSvc.SkuInfo()
                    {
                        Sku=Convert.ToInt32(txtSKU.Text),
                        UnitCost=Convert.ToDecimal(txtUnitCost.Text),
                        GrossWeight=Convert.ToDouble(txtGrossWeight.Text),
                        PrimaryCategoryId=400,
                        UnitPrice = Convert.ToDecimal(txtCalculatedLocalPrice.Text),
                    }
                    
                    },
                    WHID = 104,

                };

                CalculateDirectPriceResponse resp = client.CalculateDirectPrice(req);
                txtCalculatedUSPrice.Text = resp.SkuPriceDatas[0].DirectPrice.ToString("f2");
            }
        }

        public static T Convert2Enum<T>(object obj, T defaultValue) where T : struct, IComparable, IFormattable, IConvertible
        {
            ///【闻祖东 2012-2-18-233447】这里实在是没有办法在声明泛型T的时候约束他一定要继承自System.Enum，至少在.NET 4.0里面都还是不行的，不只是CRC遇到这个问题，
            ///网上很多帖子也是关于这个的，但是只能在运行时抛出异常，而不能在编译时控制编译。
            ///http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum
            ///http://code.google.com/p/unconstrained-melody/downloads/list

            if (obj == null)
                return defaultValue;

            #region 【闻祖东 2013-11-28-152540】这个写法在遇到输入值为int类型如123456的时候会直接返回123456，其实他并不是正确的枚举值。
            //if (typeof(T).IsEnum)
            //{
            //    T t = defaultValue;
            //    return Enum.TryParse(obj.ToString().Trim(), true, out t)
            //        ? t
            //        : defaultValue;
            //}

            //throw new Exception("【闻祖东 2012-2-18-232357】运行时异常，输入类型应该是一个枚举类型。");
            #endregion

            T result = defaultValue;
            object val = obj;
            if (obj.GetType() != typeof(Int32))
                val = obj.ToString().Trim();

            if (Enum.IsDefined(typeof(T), val))
                result = (T)Enum.Parse(typeof(T), val.ToString());

            return result;
        }
    }
}

